package handlers

import (
	"blog/configs"
	"github.com/gofiber/fiber/v2"
	"net/http"
)

// 返回回复
func returnReply(ctx *fiber.Ctx, code int, msg string, data interface{})error{
	content, err := configs.Json.Marshal(Reply{
		Code: code,
		Msg: msg,
		Data: data,
	})
	if err != nil{
		return err
	}
	ctx.Response().Header.SetContentType(fiber.MIMEApplicationJSON)
	return ctx.Status(http.StatusOK).Send(content)
}

// 返回ok
func returnOk(ctx *fiber.Ctx, data interface{})error{
	return returnReply(ctx, 1, "Ok!", data)
}

// 返回错误文本
func returnErrStr(ctx *fiber.Ctx, msg string)error{
	return returnReply(ctx, 0, msg, nil)
}

// 返回错误
func returnErr(ctx *fiber.Ctx, err error)error{
	return returnErrStr(ctx, err.Error())
}